package com.lxh.empwork.services.impl;

import com.github.pagehelper.util.StringUtil;
import com.lxh.empwork.dao.EmpMapper;
import com.lxh.empwork.entity.Emp;
import com.lxh.empwork.services.EmpServices;
import com.lxh.empwork.util.MybatisUtil;
import com.lxh.empwork.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.Map;


public class EmpServicesImpl implements EmpServices {
    @Override
    public boolean updateEmp(Emp e) {
        try {
            EmpMapper mapper = MybatisUtil.getMapper(EmpMapper.class);
            int i = mapper.updateEmp(e);
            MybatisUtil.commit();
            if (i>0) {
                return true;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            MybatisUtil.rollback();
        }
        return false;
    }

    @Override
    public Emp getEmpByEno(int eno) {
        if (eno>0) {
            EmpMapper mapper = MybatisUtil.getMapper(EmpMapper.class);
            return mapper.getEmpByEno(eno);
        }
        return null;
    }

    @Override
    public boolean addEmp(Emp e) {
        try {
            EmpMapper mapper = MybatisUtil.getMapper(EmpMapper.class);
            int i = mapper.addEmp(e);
            MybatisUtil.commit();
            if (i>0) {
                return true;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            MybatisUtil.rollback();
        }
        return false;
    }

    @Override
    public boolean delEmp(int eno) {
        try {
            if (eno > 0) {
                EmpMapper mapper = MybatisUtil.getMapper(EmpMapper.class);
                int i = mapper.delEmp(eno);
                MybatisUtil.commit();
                if (i>0){
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            MybatisUtil.rollback();
        }
        return false;
    }

    @Override
    public PageInfo<Emp> getEmpsByMapsWithPage(Map<String, String> maps, int pageNum) {
        EmpMapper mapper = MybatisUtil.getMapper(EmpMapper.class);
        // 设置分页工具
        PageHelper.startPage(pageNum, 5);
        List<Emp> list = mapper.getEmpsByMaps(maps);
        //创建PageInfo对象
        PageInfo<Emp> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public List<Emp> getEmpsByMaps(Map<String, String> parameterMap) {
        EmpMapper mapper = MybatisUtil.getMapper(EmpMapper.class);
        List<Emp> empList = mapper.getEmpsByMaps(parameterMap);
        return empList;
    }

    @Override
    public Emp doLogin(String ename, String pwd) {
        if (StringUtils.isNotEmpty(ename,pwd)) {
            EmpMapper mapper = MybatisUtil.getMapper(EmpMapper.class);
            Emp emp = mapper.getEmpByEname(ename);
            if (emp != null && emp.getPwd().equals(pwd)) {
                return emp;
            }
        }
        return null;
    }
}
